<?php

/*------------------------------------------------
*
*   Explay Engine Content Management System.
*   Copyright © 2008 Golovdinov Alexander.
*
*-------------------------------------------------
*
*   Official site: wwww.alex-home.spb.ru/explay.html
*   Contact e-mail: golovdinov@gmail.com
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
------------------------------------------------*/

if (!defined ('EXPLAY')) exit;

$number = 20;
$start = 0;
if (!empty ($_GET['page'])) $start = (int) (intval ($_GET['page']) - 1) * $number;


past_navblock (array (
	array ("Блоги", "/top/", "cat", ""),
	array ("Статьи", "/top/articles/", "cat", "articles"),
	array ("Комментарии", "/top/comments/", "cat", "comments")
));


if (isset($_GET['cat']) && ($_GET['cat'] == 'articles' || $_GET['cat'] == 'comments')) {
	// Время в днях
	if (isset($_GET['time'])) {
		switch (intval ($_GET['time'])) {
			case 1:   $time = 1; break;
			case 30:  $time = 30; break;
			case 365: $time = 365; break;
			default: $time = 7;
		}
	}
	else $time = 7;
	
	$time = (int) $time;
	
	// Форма для выбора времени
	$sel = array ();
	$sel[1] = '';
	$sel[2] = '';
	$sel[3] = '';
	$sel[4] = '';	
	
	switch ($time) {
		case 1: $sel[1] = 'selected'; break;
		case 7: $sel[2] = 'selected'; break;
		case 30: $sel[3] = 'selected'; break;
		case 365: $sel[4] = 'selected'; break;
	}
	
	Templater::load ('articles/forms.tpl');
	
	($_GET['cat'] == 'articles') 
		? Templater::set_var ('action', '/top/articles/')
		: Templater::set_var ('action', '/top/comments/');
	
	Templater::set_var ('s', $sel);
	
	print Templater::display_part ('top');
	
	
	
	// 86400
	$time_ = $time;
	$time = 86400 * $time;
}



// Топ статей
if (isset($_GET['cat']) && $_GET['cat'] == 'articles') {
	
	CORE::set_page_title ('Top статей &mdash; '.$SITE['site_name']);
	
	include $_SERVER['DOCUMENT_ROOT'].'/modules/articles/interfaces.php';
	include $_SERVER['DOCUMENT_ROOT'].'/modules/articles/controller.class.php';
	
		
	$oArticles = articlesController::getInstance();
	
	$oArticles->load_class ($_SERVER['DOCUMENT_ROOT'].'/modules/articles/model.class.php', 'articlesModel');
	
	include $_SERVER['DOCUMENT_ROOT'].'/modules/articles/mysql.class.php';
	include $_SERVER['DOCUMENT_ROOT'].'/modules/articles/view.class.php';
	
	$oArticles->load_settings ();
	
	$oArticles->set_order ('art_rating');
	
	$oArticles->add_filter ('art_date', (time() - $time), '>');
	$oArticles->add_after_url ('time', $time);
	$oArticles->set_bar_off ();
	
	$oArticles->past_articles_list ();
	$oArticles->show_result ();

}



// Топ комментариев
elseif (isset($_GET['cat']) && $_GET['cat'] == 'comments') {

	CORE::set_page_title ('Top комментариев &mdash; '.$SITE['site_name']);
	
	
	include $_SERVER['DOCUMENT_ROOT'].'/modules/comments/comments.class.php';
	
	$oComments = new commnetsList;
	$oComments->add_filter ('com_date', (time() - $time), '>');
	$oComments->add_filter ('com_category', 'articles');
	
	$oComments->build_advanced_articles_comments ($start, '?time='.$time_);
	print $oComments->show_data ();
	
	
	
// Топ блогов (по умолчанию)
} else {
	$get = $db->query ("SELECT cat_name, cat_header, cat_rating, b.bcnt, art.cnt FROM ".DB_PEREFIX."_art_category AS cat
		LEFT JOIN (SELECT COUNT(art_id) AS cnt, art_category FROM ".DB_PEREFIX."_articles WHERE art_publik = 'yes' AND art_like_page = 'no' GROUP BY art_category) AS art ON art.art_category = cat.cat_name
		LEFT JOIN (SELECT COUNT(*) AS bcnt, blog FROM ".DB_PEREFIX."_blogs_users GROUP BY blog) AS b ON b.blog = cat.cat_id
		ORDER BY cat_rating DESC LIMIT $start, $number");
	
	Templater::load ('articles/blogs_top.tpl');
	
	$list = '';	
	
	while ($blog = $db->fetch_array ($get)) {
		
		if ($blog['cnt'] == '') $blog['cnt'] = 0;
		$blog['cat_rating'] = number_format ($blog['cat_rating'], 2, ',', '');
		
		Templater::set_var ('blog', $blog);
		
		$list .= Templater::display_part ('list', false);	
	}
	
	Templater::set_var ('list', $list);
	print Templater::display_part ('main');	
	
	$count = $db->num_rows ($db->query ("SELECT * FROM ".DB_PEREFIX."_art_category"));
	if ($count > $number) print past_pages($start, $number, $count, '/top/');
}